#include<iostream>

using namespace std;

int excgcd(int a,int b,int &x,int &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    int d=excgcd(b,a%b,y,x);
    y-=a/b*x;
    
    return d;
}

int main()
{
    int n;scanf("%d",&n);
    
    while(n--)
    {
        int a,b,m,x,y;
        scanf("%d%d%d",&a,&b,&m);
        
        int d=excgcd(a,m,x,y);
        if(b%d!=0) cout<<"impossible"<<endl;
        else cout<<(long long)x*b/d%m<<endl;
    }
    return 0;
}